import javafx.scene.control.SelectionMode;

import java.sql.Statement;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2024-05-22
 * Time: 11:30
 */
public class Sort {
    // 插入排序

    /**
     *时间复杂度:最好情况下:有序,O(N)
     *                  最坏情况下:逆序,O(N*N)
     * 空间复杂度:O(1)
     * 稳定性:稳定
     * @param array
     */
    public static void insertSort(int[] array){
            // write code  here
        for (int i = 1; i < array.length; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j>=0 ; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];

                }else{
                    array[j+1] = tmp;
                    break;
                }


            }
            array[j+1] = tmp;
        }
        }


            // 希尔排序

    /**
     * 时间复杂度:O(N ^1.3)
     * 空间复杂度:O(1)
     * 稳定性: 不稳定
     * @param array
     */
    public static void shellSort(int[] array){
            // write code  here
        int gap = array.length;
        while(gap > 1){
            gap = gap/2; // 5 , 2 ,1
            shell(array , gap);
        }


        }
        private static void shell(int[] array,int gap){
            for (int i = gap; i < array.length; i++) {
                int tmp= array[i] ;
                int j = i-gap;
                for (; j >= 0  ; j-=gap) {
                    if(array[j] > tmp){
                        array[j+gap] = array[j];

                    }else{
                        break;
                    }
                }
                array[j+gap] = tmp;
            }
        }
        // 选择排序

    /**
     * 时间复杂度:O(N^2)
     * 空间复杂度:O(1)
     * 不稳定
     * @param array
     */
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length; i++) {
            int tmp = i;int j = i+1;
            for (; j < array.length ; j++) {
                if(array[j] < array[tmp]){
                    tmp = j;
                }
            }
            swap(array,i,tmp);
        }

    }

    public static void selectSort2(int[] array){
        int left =0 ;
        int right = array.length-1;
      while(left < right) {
          int minIndex = left;
          int maxIndex = left;
          for (int i = left+1 ; i <= right; i++) {
              if(array[i] < array[minIndex]){
                  minIndex = i;
              }else if(array[i] > array[maxIndex]){
                  maxIndex= i;
              }


          }
          swap(array, left,minIndex);
          if(maxIndex == left){
              maxIndex = minIndex;
          }
          swap(array,right,maxIndex);
          left ++;
          right--;
      }
    }

    private static void swap(int[] array , int i, int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    // 堆排序
    public static void heapSort(int[] array){
            // write code  here
        createBigHeap(array);
        for (int i = array.length-1; i >0 ; i--) {
            swap(array,0 , i);
            shiftDown(array, 0, i-1);
        }
   }

    private static void shiftDown(int[] array, int parent, int len) {
        int child = parent*2 +1;
        while(child <len){
            if(child+1 < len && array[child] < array[child+1]){
                child++;
            }
            if(array[parent] < array[child]){
                swap(array , parent ,child);
                parent = child;
                child= parent*2 +1;
            }else{
                break;
            }
        }
    }

    public  static void createBigHeap(int[] array){
            for (int parent = (array.length-1-1)/2; parent >=0; parent--) {
                int child = parent*2 +1;
                while(child < array.length){
                    if(child+1 < array.length && array[child] < array[child+1]){
                        child++;
                    }
                    if(array[parent] < array[child]){
                        swap(array , parent ,child);
                        parent = child;
                        child= parent*2 +1;
                    }else{
                        break;
                    }
                }
            }

        }
        // 冒泡排序

    /**
     * 时间复杂度:O(N^2)
     *空间复杂度:O(1)
     * 稳定性:稳定
     * @param array
     */
    public static void bubbleSort(int[] array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = true;
            for (int j = 0; j <array.length-1-i ; j++) {
                if(array[j+1] <array[j]){
                    swap(array ,j , j+1);
                    flg = false;
                }
            }
            if(flg){
                break;
            }
        }
        }

    // 快速
    public static void quickSort(int[] array){

        quick(array , 0 , array.length-1);




    }

    private static void quick(int[] array, int left, int right) {
        if(left>= right){
            return;
        }
        /**
         * 快排:
         * 优化一:
         * 三数取中法
         */
        int mid = findMiddle(array,left,right );
        swap(array,mid,left);
        int pivot = find2(array,left , right);
        /**
         * 快排优化二:
         *
         */
        if(right -left >= 10){
            insertSort2(array,left , right);
        }
        quick(array,left , pivot-1);
        quick(array,pivot+1 , right);
    }
    private static  void insertSort2(int[] array ,int  left , int right){
        for (int i = left+1; i <= right; i++) {
            int tmp = array[i];
            int j = i-1;
            for (; j>=left; j--) {
                if(array[j] > tmp){
                    array[j+1] = array[j];
                }else{
                    array[j+1] = tmp;
                    break;
                }


            }
            array[j+1] = tmp;
        }
    }
    private static int findMiddle(int[] array ,int left,int right){
        int mid = left+(right - left)/2;
        if(array[left] > array[right]){
            if(array[mid] > array[left]){
                return left;
            }else if (array[mid] < array[right]){
                return right;
            }else{
                return mid;
            }
        }else{
            if(array[mid] > array[right]){
                return right;
            }else if(array[mid] < array[left]){
                return left;
            }else{
                return mid;
            }
        }
    }
    private static int find(int[] array , int left , int right){
        int tmp = array[left];
        int i = left;
        while(left < right){
            while( left < right && array[right] >= tmp){
                right--;
            }
            while (left<right && array[left] <= tmp){
                left ++;
            }
            swap(array,left,right);
        }
        swap(array,i,left);
        return left;

    }
    //挖坑法
    private static int find2(int[] array,int left , int right){
        int tmp = array[left];
        while(left < right){

            while(left <right && array[right] >= tmp){
                right--;
            }
            array[left] = array[right];
             while(left <right && array[left] <=  tmp){
                left++;
            }
             array[right] = array[left] ;
        }
        array[left] = tmp;
        return left ;
    }

    /**
     * 快排:
     * 非递归实现
     * @param array
     */
    public static void quickSort2(int[] array){
        quick2(array, 0 ,array.length-1);

    }
    private static void quick2(int[] array , int left , int right){
        Stack<Integer> stack = new Stack<>();
        int pivot =find(array,left, right);
        if(pivot-1 > left){
            stack.push(left);
            stack.push(pivot-1);
        }
        if(pivot+1 < right){
            stack.push(pivot+1);
            stack.push(right);
        }
        while(! stack.isEmpty()){
            right = stack.pop() ;
            left = stack.pop();
            pivot = find(array,left, right);
            if(pivot-1 > left){
                stack.push(left);
                stack.push(pivot-1);
            }
            if(pivot+1 < right){
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

    //归并排序
// 归并排序---递归
    public static void mergeSort1(int[] array){
        merge(array, 0 ,array.length-1);

    }
    private static void merge(int[] array , int left ,int right){
        if(left >= right){
            return;
        }
        int mid = left + (right - left) /2;
        merge(array,left , mid);
        merge(array,mid+1,right);
        func(array,left , mid,right);

    }
    private static void func(int[] array , int left,int mid ,int right){
        int s1 = left;
        int e1= mid;
        int s2 = mid+1;
        int e2 = right;
        int[] tmpArr = new int[right-left+1];
        int k=0;
        while(s1 <= e1 && s2 <= e2){
            if(array[s1] < array[s2]){
                tmpArr[k] = array[s1];
                k++;
                s1++;
            }else{
                tmpArr[k] = array[s2];
                k++;
                s2++;
            }
        }
        while(s1 <=e1){
            tmpArr[k] = array[s1];
            k++;
            s1++;
        }
        while(s2 <= e2){
            tmpArr[k] = array[s2];
            k++;
            s2++;
        }
        for (int i = 0; i < k; i++) {
            array[left+i] = tmpArr[i];


        }
    }

    // 归并排序---非递归
    public static void mergeSort2(int[] array){
        int gap =1;
        while(gap < array.length){
            for (int i = 0; i < array.length; i= i+2*gap) {
                int s1 = i;
                int e1 = i+gap-1;
//                int s2 =i+gap;
                int e2 =e1+gap;
                if(e1 > array.length-1 ){
                    e1  = array.length-1;
                }
                if(e2 > array.length-1){
                    e2 = array.length-1;
                }
                func(array,s1,e1,e2);
            }
            gap*=2;
        }
        }

    // 计数排序
    public static void countSort(int[] array){

        int min =array[0];
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i] < min){
                min = array[i];
            }
            if(array[i] > max){
                max = array[i];
            }
        }
        int[] count = new int[max - min+1];
        for (int i = 0; i < array.length; i++) {
            int index =array[i] -min;
            count[index]++;

        }
        int k =0;
        for (int i = 0; i < count.length; i++) {
            while(count[i] > 0){
                array[k] = i +min;
                k++;
                count[i]--;
            }
        }

    }

}
